package server.exchange;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import sharedrequest.AllocateIDRequest;
import sharedrequest.Request;
import sharedresponse.AllocateIDResponse;


public class RNT_Server {

	public static void main(String[] args) {
		Logger serverLogger=Logger.getLogger(RNT_Server.class.getName());
		List <ServerReadedThread> workedThreads=new ArrayList<ServerReadedThread>();
		
		Dispatcher dispatcher1=new Dispatcher();
		try {
			ServerSocket serversocket=new ServerSocket(2013);
			serverLogger.info("Server starts...");
			while (true) {
				Socket connectedClientSocket=serversocket.accept();
				serverLogger.info("Accepted!");
				InputStream is=connectedClientSocket.getInputStream();
				ObjectInputStream ois=new ObjectInputStream(is);
				ObjectOutputStream oos=new ObjectOutputStream(connectedClientSocket.getOutputStream());
				try{
					System.out.println("Hello");
					Object firstMessageFromClient=ois.readObject();
					if (!(firstMessageFromClient instanceof AllocateIDRequest)) throw new ClassNotFoundException();
					AllocateIDRequest firstRequest=(AllocateIDRequest)firstMessageFromClient;
					String clientName=firstRequest.getUsername();
					FullClientInfo.addClient(clientName, connectedClientSocket);
					dispatcher1.addStock(firstRequest.getStockName());
					dispatcher1.doSomething(firstRequest);
					ServerReadedThread server=new ServerReadedThread(dispatcher1, ois,oos);
					server.start();
					workedThreads.add(server);
					Thread.sleep(2000);

				} catch (ClassNotFoundException e) {
					serverLogger.info("First reading from client Error");
				} catch (InterruptedException e){
					serverLogger.info("Interrupted Exception in server");
				}
			}
			
		} catch (IOException e) {
			serverLogger.info("Error in server creation");
		}

//		Dispatcher disp1=new Dispatcher();
//		//AllocateIdRequest allocateId0= new AllocateIdRequest();
//		//disp1.doSomething(allocateId0);
////		ListenerThread listener = new ListenerThread();
////		listener.start();
//		
//		Client cl1=new Client("Client A");
//		Order ord1=new Order(cl1, OPERATIONTYPE.BUY,"Spartak",200,(float)3.234,1);
//		PlaceOrderRequest placeOrder1= new PlaceOrderRequest(ord1,ord1.getClientName());
//		disp1.doSomething(placeOrder1);
//		Client cl2=new Client("Client B");
//		Order ord2=new Order(cl2, OPERATIONTYPE.BUY,"Spartak",400,(float)3.234,2);
//		PlaceOrderRequest placeOrder2= new PlaceOrderRequest(ord2,ord2.getClientName());
//		disp1.doSomething(placeOrder2);
//		Client cl3=new Client("Client C");
//		Order ord3=new Order(cl3, OPERATIONTYPE.SELL,"Spartak",500,(float)3.234,3);
//		PlaceOrderRequest placeOrder3= new PlaceOrderRequest(ord3, ord3.getClientName());
//		disp1.doSomething(placeOrder3);
////		Order ord4=new Order(cl3, OPERATIONTYPE.SELL,"Spartak",100,(float)3.234,4);
////		PlaceOrderRequest placeOrder4= new PlaceOrderRequest(ord4);
////		disp1.doSomething(placeOrder4);
//		
//		CancelOrderRequest cancelOrder1= new CancelOrderRequest("Spartak","Client C",3);
//		disp1.doSomething(cancelOrder1);
//		Logger log1=Logger.getLogger("my firs logger");
//		log1.addHandler(new ConsoleHandler());
//		log1.info("Yo!");
//		try{
//			BigServer bs1=new BigServer();
//			ServerThread st=new ServerThread(bs1);
//			st.start();
//		} catch (IOException e){
//			System.out.println("1");
//		}
//		
//		try{
//			BigClient bc2=new BigClient();
//			ClientThread ct=new ClientThread(bc2);
//			ct.start();
//		} catch (IOException e){
//
//		System.out.println("2");
//		}
		

	}

}
class ServerReadedThread extends Thread{
	private Dispatcher dispatcher;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;
	Logger serverReadLogger=Logger.getLogger(ServerReadedThread.class.getName());
	
	public ServerReadedThread(Dispatcher dispatcher,ObjectInputStream ois, ObjectOutputStream oos){
		this.dispatcher=dispatcher;
		this.ois=ois;
		this.oos=oos;
	}
	
	public void run(){
		try{
			while(true){
				try{
					serverReadLogger.info("Reading..");
					Request request=(Request)ois.readObject();
					serverReadLogger.info("Readed");
					oos.writeObject("Hello");
					oos.flush();
					
					dispatcher.doSomething(request);
						
					
				} catch (ClassNotFoundException e){
					serverReadLogger.info("Error in reading from Client");
				}
			}
		} catch (IOException e){
			serverReadLogger.info("Client disconnected");
		}
	}
}






